package com.ykw.parking.controller;

import com.ykw.parking.mapper.UserMapper;
import com.ykw.parking.pojo1.Bank;
import com.ykw.parking.pojo1.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class bindBankController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/bindBank")
    public String bindBank(){
        return "bindBank";
    }

    @RequestMapping(value = "/addBank",method = RequestMethod.POST)
    @ResponseBody
    public String addBank(String card,HttpSession session){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        userMapper.insertBank(card,user.getId());
        return "1";
    }

    @RequestMapping(value = "/getBank")
    @ResponseBody
    public List<Bank> getBank(HttpSession session){
        List<Bank> banks=new ArrayList<>();
        String username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        Bank bank = userMapper.getBank(user.getId());
        banks.add(bank);
        return banks;
    }

    @RequestMapping(value = "/addMoney",method = RequestMethod.POST)
    @ResponseBody
    public Double addMoney(double money,HttpSession session){
        String username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        userMapper.addMoney(money,user.getId());

        return userMapper.getMoney(user.getId());
    }
}
